home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / tjgold.zip / INSTALL.002 / GOLDMISC.PAS < prev    next >
Pascal/Delphi Source File  |  1995-07-12  |  25KB  |  1,001 lines

  1. {--------------------------------------------------------------------------}
  2. {                Product: TechnoJock's Turbo Toolkit                       }
  3. {                Version: GOLD                                             }
  4. {                Build:   1.01                                             }
  5. {                                                                          }
  6. {                Copyright 1986-1995  TechnoJock Software, Inc.            }
  7. {                           All Rights Reserved                            }
  8. {                          Restricted by License                           }
  9. {--------------------------------------------------------------------------}
  10.  
  11.                     {**********************************}
  12.                     {**       Unit:   GOLDMISC       **}
  13.                     {**********************************}
  14.  
  15. {++++++++++++++++++++++++++++++} unit GOLDMISC;{+++++++++++++++++++++++++++++}
  16.  
  17. {$I GOLDFLAG.INC}
  18. {$IFNDEF GOLDMISC}
  19.    {$DEFINE GOLDMISC}
  20. {$ENDIF}
  21.  
  22. {++++++++++++++++++++++++++++++++} INTERFACE {+++++++++++++++++++++++++++++++}
  23.  
  24. Uses CRT, DOS, GoldStr;
  25.  
  26. const
  27.    Illegal: string[15] = ' +=/[]":;,?*<>|';
  28.  
  29. type
  30.    ErrMsgFunc = function (Ecode:integer):string;
  31.  
  32.    gCoords = record
  33.       X1,Y1,X2,Y2:shortint;
  34.    end;
  35.  
  36.    gByteCoords = record
  37.       X1,Y1,X2,Y2:byte;
  38.    end;
  39.  
  40.    MiscSet = record
  41.       ECode: integer;
  42.       GoldMemBuffer: longint;
  43.       StartMem: longint;
  44.       HeapIsRecorded,
  45.       BeepOn: boolean;
  46.       LPTport:byte;     {0=lpt1, 1=lpt2, 2=lpt3}
  47.       StartTop,      {used to record initial screen state when program is run}
  48.       StartBot: byte;
  49.       StartMode: word;
  50.       EMsgFunc: ErrMsgFunc;
  51.       HeapCheckErrMsg: string[80];
  52.    end;
  53.  
  54. var
  55.    MiscVars: MiscSet;
  56.  
  57. function  LastMiscError: integer;
  58. function  GetBitStatus(B:byte;BitPos:byte): boolean;
  59. procedure SetBitStatus(var Val:byte; BitPos:byte; On:boolean);
  60. function  GoldMaxAvail:longint;
  61. function  GoldMemAvail:longint;
  62. procedure Swap(var A,B:longint);
  63. function  WithinRange(Min,Max,Test: longint): boolean;
  64. function  OnBorder(X1,Y1,X,Y,Width,Depth:integer):boolean;
  65. function  WithinBorder(X1,Y1,X,Y,Width,Depth:integer):boolean;
  66. procedure Beep;
  67. procedure Ding;
  68. procedure Thunk;
  69. procedure Trill;
  70. function  FSize(Filename:string): longint;
  71. function  FileDrive(Full:string): string;
  72. function  FileExt(Full:string): string;
  73. function  SlashedDirectory(Dir:string):string;
  74. function  ParentDirectory(Dir:string): string;
  75. function  FileDirectory(Full:string): string;
  76. function  SmartMakeDir(DirectStr: PathStr): integer;
  77. function  FileName(Full:string): string;
  78. function  Exist(Filename:string):boolean;
  79. function  DeleteFile(Filename:string): shortint;
  80. function  RenameFile(Oldname,NewName:string):shortint;
  81. function  CopyFile(SourceFile, TargetFile:string): shortint;
  82. function  PrinterStatus:byte;
  83. function  AlternatePrinterStatus:byte;
  84. function  PrinterReady :boolean;
  85. procedure ResetPrinter;
  86. procedure PrintScreen;
  87. function  ParamLine: String;
  88. function  ParamVal(Marker:string): string;
  89. function  Frequency(Match:string;Source:string): byte;
  90. function  BadCharPos(Str:string): integer;
  91. function  ValidFileName(FN:string): shortint;
  92. function  RunEXECOM(Progname, Params: string):integer;
  93. function  RunDOS(Msg:string):integer;
  94. function  RunAnything(Command: string):integer;
  95. function  GetMin(Value1,Value2:longint): longint;
  96. function  GetMax(Value1,Value2:longint): longint;
  97. procedure HeapRecord;
  98. procedure HeapCheck;
  99.  
  100. {$IFDEF TTT5}
  101.  
  102. function  File_Size(Filename:string): longint;
  103. function  File_Drive(Full:string): string;
  104. function  File_Directory(Full:string): string;
  105. function  File_Name(Full:string): string;
  106. function  File_Ext(Full:string): String;
  107. function  Printer_Status:byte;
  108. function  Alternate_Printer_Status:byte;
  109. function  Printer_ready:boolean;
  110. procedure Reset_Printer;
  111.  
  112. {$ENDIF}
  113.  
  114. {+++++++++++++++++++++++++++++} IMPLEMENTATION {+++++++++++++++++++++++++++++}
  115. {$IFOPT F-}
  116.    {$DEFINE FOFF}
  117.    {$F+}
  118. {$ENDIF}
  119.  
  120. function MiscEMsg(ECode:integer): string;
  121. {}
  122. begin
  123.    case Ecode of
  124.       1001: MiscEMsg := 'Invalid drive passed to SmartMakeDir';
  125.       1002: MiscEMsg := 'Failure changing directories';
  126.       1003: MiscEMsg := 'Failure making directories';
  127.       else
  128.          MiscEMsg := 'Internal Misc error';
  129.    end; {case}
  130. end; { MiscEMsg }
  131.  
  132. {$IFDEF FOFF}
  133.    {$F-}
  134.    {$UNDEF FOFF}
  135. {$ENDIF}
  136.  
  137. procedure MiscSetError(ECode:integer);
  138. {}
  139. {$IFOPT D+}
  140. var Ch: char;
  141.     Msg: string;
  142. {$ENDIF}
  143. begin
  144.    MiscVars.Ecode := ECode;
  145. {$IFOPT D+}  {if debug active display an error message and terminate}
  146.    if Ecode <> 0 then
  147.    begin
  148.       str(Ecode,Msg);
  149.       Msg := Msg+': '+MiscVars.EMsgFunc(Ecode);
  150.       writeln(' GoldMisc Error - ',Msg);
  151.       Ch := ReadKey;
  152.       if Ch = #27 then
  153.          Halt;
  154.    end;
  155. {$ENDIF}
  156. end; { MiscSetError }
  157.  
  158. function LastMiscError: integer;
  159. {}
  160. begin
  161.    LastMiscError := MiscVars.ECode;
  162. end; { LastMiscError }
  163.  
  164. procedure Swap(var A,B:longint);
  165. {}
  166. var Temp: longint;
  167. begin
  168.    Temp := A;
  169.    A := B;
  170.    B := Temp;
  171. end; { Swap }
  172.  
  173. function WithinRange(Min,Max,Test: longint): boolean;
  174. {}
  175. begin
  176.    if Min > Max then
  177.       Swap(Min,Max);
  178.    WithinRange := (Test >= Min) and (Test <= Max);
  179. end; { WithinRange }
  180.  
  181. function GetBitStatus(B:byte;BitPos:byte): boolean;
  182. {}
  183. begin
  184.    if BitPos > 7 then
  185.       GetBitStatus := false
  186.    else
  187.    begin
  188.      B := B SHR BitPos; {move to end bit}
  189.      GetBitStatus := odd(B);
  190.    end;
  191. end; { GetBitStatus }
  192.  
  193. procedure SetBitStatus(var Val:byte; BitPos:byte; On:boolean);
  194. {}
  195. var Test: integer;
  196. begin
  197.    if BitPos <= 7 then
  198.    begin
  199.       if On then
  200.       begin
  201.          Test := 1 SHL BitPos;
  202.          Val := Val or Test
  203.       end else
  204.       begin
  205.          Test := not (1 SHL BitPos);
  206.          Val := Val and Test;
  207.       end;
  208.    end;
  209. end; { SetBitStatus }
  210.  
  211. function GoldMaxAvail:longint;
  212. {}
  213. begin
  214.    GoldMaxAvail := MaxAvail - MiscVars.GoldMemBuffer;
  215. end; { GoldMaxAvail }
  216.  
  217. function GoldMemAvail:longint;
  218. {}
  219. begin
  220.    GoldMemAvail := MemAvail - MiscVars.GoldMemBuffer;
  221. end; { GoldMemAvail }
  222.  
  223. function OnBorder(X1,Y1,X,Y,Width,Depth:integer):boolean;
  224. {}
  225. begin
  226.    OnBorder := ( ((X1 >= X) and (X1 < X + Width))
  227.                   and
  228.                  ((Y1 = Y) or (Y1 = pred(Y+Depth)))
  229.                )
  230.                or
  231.                ( ((Y1 >= Y) and (Y1 < Y + Depth))
  232.                  and
  233.                  ((X1 = X) or (X1 = pred(X+Width)))
  234.                );
  235. end; { OnBorder }
  236.  
  237. function WithinBorder(X1,Y1,X,Y,Width,Depth:integer):boolean;
  238. {}
  239. begin
  240.    WithinBorder := ((X1 >= X) and (X1 < X + Width))
  241.                    and
  242.                    ((Y1 >= Y) and (Y1 < Y + depth));
  243. end; { WithinBorder }
  244.  
  245. procedure Beep;
  246. {}
  247. begin
  248.    if MiscVars.BeepOn then
  249.    begin
  250.       sound(800);delay(150);
  251.       sound(600);delay(100);
  252.       nosound;
  253.    end;
  254. end; { Beep }
  255.  
  256. procedure DING;
  257. begin
  258.    if MiscVars.BeepOn then
  259.    begin
  260.       sound(2000);delay(100);nosound;
  261.    end;
  262. end; { Ding }
  263.  
  264. procedure Thunk;
  265. {}
  266. begin
  267.    if MiscVars.BeepOn then
  268.    begin
  269.       sound(100);delay(150);
  270.       sound(250);delay(10);
  271.       nosound;
  272.    end;
  273. end; { Thunk }
  274.  
  275. procedure Trill;
  276. {}
  277. begin
  278.    if MiscVars.BeepOn then
  279.    begin
  280.       sound(880); delay(100);
  281.       sound(1320); delay(100);
  282.       sound(1760); delay(100);
  283.       sound(2200); delay(100);
  284.   nosound;
  285.    end;
  286. end; { Trill }
  287.  
  288.  
  289. function CopyFile(SourceFile, TargetFile:string): shortint;
  290. {return codes:  0 successful
  291.                 1 source and target the same
  292.                 2 cannot open source
  293.                 3 unable to create target
  294.                 4 error during copy
  295. }
  296. var Source,
  297.     Target: file;
  298.     BRead,
  299.     Bwrite: word;
  300.     FileBuf: array[1..2048] of char;
  301. begin
  302.    if SourceFile = TargetFile then
  303.       CopyFile := 1
  304.    else
  305.    begin
  306.       assign(Source,SourceFile);
  307.       {$I-}
  308.       reset(Source,1);
  309.       {$I+}
  310.       if IOResult <> 0 then
  311.           CopyFile := 2
  312.       else
  313.       begin
  314.          Assign(Target,TargetFile);
  315.          {$I-}
  316.          Rewrite(Target,1);
  317.          {$I+}
  318.          if IOResult <> 0 then
  319.             CopyFile := 3
  320.          else
  321.          begin
  322.             repeat
  323.               blockread(Source,FileBuf,SizeOf(FileBuf),BRead);
  324.               blockwrite(Target,FileBuf,Bread,Bwrite);
  325.             until (Bread = 0) or (Bread <> BWrite);
  326.             close(Source);
  327.             close(Target);
  328.             if Bread <> Bwrite then
  329.                CopyFile := 4
  330.             else
  331.                CopyFile := 0;
  332.          end;
  333.       end;
  334.    end;
  335. end; { CopyFile }
  336.  
  337. function FSize(Filename:string): longint;
  338. {returns  -1   if file not found}
  339. var FileInfo: SearchRec;
  340. begin
  341.    Findfirst(Filename,anyfile,FileInfo);
  342.    if DOSError = 0 then
  343.       FSize := FileInfo.Size
  344.    else
  345.       FSize := -1;
  346. end; { FSize }
  347.  
  348. function FileSplit(Part:byte;Full:string): string;
  349. {used internally}
  350. var D: DirStr;
  351.     N: NameStr;
  352.     E: ExtStr;
  353. begin
  354.    FSplit(Full,D,N,E);
  355.    Case Part of
  356.       1: FileSplit := D;
  357.       2: FileSplit := N;
  358.       3: FileSplit := E;
  359.    end;
  360. end; { FileSplit }
  361.  
  362. function FileDirectory(Full:string): string;
  363. {}
  364. var Temp: string;
  365.     P: byte;
  366. begin
  367.    Temp := FileSplit(1,Full);
  368.    P := Pos(':',Temp);
  369.    if P = 2 then
  370.       Delete(Temp,1,2);                 {remove drive}
  371.    if (Temp[length(Temp)]  ='\') and (temp <> '\') then
  372.       Delete(temp,length(Temp),1);      {remove last backslash}
  373.    FileDirectory := Temp;
  374. end; { FileDirectory }
  375.  
  376. function SmartMakeDir(DirectStr: PathStr): integer;
  377. {creates multi-level subdirectories}
  378. var I, P: byte;
  379.     Drv, Dir, SavedPath: PathStr;
  380. begin
  381.    SmartMakeDir := 1;
  382.    getdir(0,SavedPath);
  383.    if DirectStr[2] = ':' then
  384.    begin
  385.       Drv := copy(DirectStr,1,2);
  386.       {$I-} chdir(Drv); {$I+}
  387.       if IOResult <> 0 then
  388.       begin
  389.          MiscSetError(1001); { invalid drive passed to MakeDir }
  390.          chdir(SavedPath);
  391.          exit;
  392.       end;
  393.       delete(DirectStr,1,2);
  394.    end
  395.    else
  396.      Drv := copy(SavedPath,1,2);
  397.    if DirectStr[1] = '\' then
  398.    begin
  399.       {$I-} chdir(Drv+'\'); {$I+}
  400.       if IOResult <> 0 then
  401.       begin
  402.          MiscSetError(1002); { Failure changing directories }
  403.          chdir(SavedPath);
  404.          exit;
  405.       end;
  406.       delete(DirectStr,1,1);
  407.    end;
  408.    if DirectStr[length(DirectStr)] <> '\' then
  409.       DirectStr := DirectStr + '\';
  410.    while length(DirectStr) > 0 do
  411.    begin
  412.       P := pos('\',DirectStr);
  413.       Dir := copy(DirectStr,1,pred(P));
  414.       delete(DirectStr,1,P);
  415.       {$I-} chdir(Dir); {$I+}
  416.       if IOResult <> 0 then  { if it doesn't exist then make it }
  417.       begin
  418.          {$I-} mkdir(Dir); {$I+}
  419.          if IOResult <> 0 then
  420.          begin
  421.             MiscSetError(1003); { Failure making directories }
  422.             chdir(SavedPath);
  423.             exit;
  424.          end
  425.          else
  426.          begin
  427.            {$I-} chdir(Dir); {$I+}  { then change to it }
  428.            if IOResult <> 0 then
  429.            begin
  430.               MiscSetError(1002); { Failure changing directories }
  431.               chdir(SavedPath);
  432.               exit;
  433.            end;
  434.         end;
  435.       end;
  436.    end;
  437.    {$I-} chdir(SavedPath); {$I+}
  438.    if IOResult <> 0 then     ;
  439.       { Set error }
  440.    SmartMakeDir := 0;
  441. end; { SmartMakeDir }
  442.  
  443. function FileName(Full:string): string;
  444. {}
  445. begin
  446.    FileName := FileSplit(2,Full);
  447. end; { FileName }
  448.  
  449. function FileExt(Full:string): string;
  450. {}
  451. var Temp: string;
  452. begin
  453.    Temp := FileSplit(3,Full);
  454.    if (Temp = '') or (Temp = '.') then
  455.       FileExt := temp
  456.    else
  457.       FileExt := copy(Temp,2,3);
  458. end; { FileExt }
  459.  
  460. function SlashedDirectory(Dir:string):string;
  461. {}
  462. begin
  463.    if (Dir = '') or (Dir[length(Dir)] in [':','\']) then
  464.       SlashedDirectory := Dir
  465.    else
  466.       SlashedDirectory := Dir + '\';
  467. end; { SlashedDirectory }
  468.  
  469. function ParentDirectory(Dir:string): string;
  470. {}
  471. var L:byte;
  472. begin
  473.    L := length(Dir);
  474.    if L < 4 then
  475.       ParentDirectory := Dir
  476.    else
  477.    begin
  478.       if Dir[L] = '\' then
  479.          delete(Dir,L,1);
  480.       L := lastPos('\',Dir);
  481.       if L = 0 then
  482.          L := pos(':',Dir);
  483.       if L = 0 then
  484.          ParentDirectory := ''
  485.       else
  486.          ParentDirectory := copy(Dir,1,pred(L));
  487.    end;
  488. end; { ParentDirectory }
  489.  
  490. function FileDrive(Full:string): string;
  491. {}
  492. var Temp: string;
  493.     P: byte;
  494. begin
  495.    Temp := FileSplit(1,Full);
  496.    P := Pos(':',Temp);
  497.    if P <> 2 then
  498.       FileDrive := ''
  499.    else
  500.       FileDrive := upcase(Temp[1]);
  501. end; { FileDrive }
  502.  
  503. function Exist(Filename:string):boolean;
  504. {returns true if file exists}
  505. var Inf: SearchRec;
  506. begin
  507.   findfirst(Filename,AnyFile,Inf);
  508.   Exist := (DOSError = 0);
  509. end;  { Exist }
  510.  
  511. function DeleteFile(Filename:string): shortint;
  512. {Return codes:   -1    File not found
  513.                   0    File deleted
  514.                   1    Error - file not deleted.
  515.  
  516. }
  517. var F: file;
  518. begin
  519.    if not Exist(Filename) then
  520.       DeleteFile := -1
  521.    else
  522.    begin
  523.       assign(F,Filename);
  524.       {$I-}
  525.       Erase(F);
  526.       {$I+}
  527.       if ioresult = 0 then
  528.          DeleteFile := 0
  529.       else
  530.          DeleteFile := 1;
  531.    end;
  532. end; { DeleteFile }
  533.  
  534. function RenameFile(Oldname,NewName:string):shortint;
  535. {Retcodes:     0 file renamed
  536.                1 file not found
  537.                2 rename failed
  538. }
  539. var F:file;
  540. begin
  541.    if not exist(OldName) then
  542.       RenameFile := 1
  543.    else
  544.    begin
  545.       assign(F,Oldname);
  546.       {$I-}
  547.       Rename(F,Newname);
  548.       {$I+}
  549.       if ioresult = 0 then
  550.          RenameFile := 0
  551.       else
  552.          RenameFile := 2;
  553.    end;
  554. end; { RenameFile }
  555.  
  556. function PrinterStatus:byte;
  557. {Credits: Robert W. Lewis, VA thanks! Special masking employed for non-
  558.           standard printers, e.g. daisy wheels!!! }
  559. var Recpack : registers;
  560. begin
  561.    with Recpack do
  562.    begin
  563.       Ah := 2;
  564.       Dx := MiscVars.LPTport;
  565.       intr($17,recpack);
  566.       if (Ah and $B8) = $90 then
  567.          PrinterStatus := 0        {all's well}
  568.       else if (Ah and $20) = $20 then
  569.          PrinterStatus := 1        {no Paper}
  570.       else if (Ah and $10) = $00 then
  571.          PrinterStatus := 2        {off line}
  572.       else if (Ah and $80) = $00 then
  573.          PrinterStatus := 3        {busy}
  574.       else if (Ah and $08) = $08 then
  575.          PrinterStatus := 4;       {undetermined error}
  576.    end;
  577. end; { PrinterStatus }
  578.  
  579. function AlternatePrinterStatus:byte;
  580. {}
  581. var Recpack: registers;
  582. begin
  583.    with recpack do
  584.    begin
  585.       Ah := 2;
  586.       Dx := MiscVars.LPTport;
  587.       intr($17,recpack);
  588.       if (Ah and $20) = $20 then
  589.          AlternatePrinterStatus := 1  {no Paper}
  590.       else if (Ah and $10) = $00 then
  591.             AlternatePrinterStatus := 2  {off line}
  592.       else if (Ah and $80) = $00 then
  593.             AlternatePrinterStatus := 3  {busy}
  594.       else if (Ah and $08) = $08 then
  595.             AlternatePrinterStatus := 4  {undetermined error}
  596.       else
  597.           AlternatePrinterStatus := 0    {all's well}
  598.    end;
  599. end; { AlternatePrinterStatus }
  600.  
  601. function PrinterReady :boolean;
  602. {}
  603. begin
  604.    PrinterReady := (PrinterStatus = 0);
  605. end; { PrinterReady }
  606.  
  607. procedure ResetPrinter; {1.1}
  608. {}
  609. var address: ^integer;
  610.     portno,delay: integer;
  611. begin
  612. {$IFDEF DPMI}
  613.    address := ptr(seg0040,$0008);
  614. {$ELSE}
  615.    address := ptr($0040,$0008);
  616. {$ENDIF}
  617.    portno := address^ + 2;
  618.    port[portno] := 232;
  619.    for delay := 1 to 2000 do {nothing};
  620.    port[portno] := 236;
  621. end; { ResetPrinter }
  622.  
  623. procedure PrintScreen;
  624. {}
  625. var Regpack: registers;
  626. begin
  627.    intr($05,regpack);
  628. end; { PrintScreen }
  629.  
  630. {IMPORTANT NOTE: You must use the $M compiler directive to instruct Turbo
  631. Pascal to leave some memory for the spawned or child program, e.g.
  632. $M $8192,$8192,$8192. The precise values depend on the size of your program
  633. ..experiment. If the child process runs OK, try smaller values.}
  634.  
  635. function RunAnything(command: string):integer;
  636. {}
  637. var Comspec:string;
  638. begin
  639.    Comspec := GetEnv('COMSPEC');
  640.    swapvectors;
  641.    exec(comspec,'/C '+command);
  642.    SwapVectors;
  643.    RunAnything := doserror;
  644. end; { RunAnything }
  645.  
  646. function RunEXECOM(Progname, Params: string): integer;
  647. {}
  648. begin
  649.    swapvectors;
  650.    exec(Progname,Params);
  651.    swapvectors;
  652.    RunEXECOM := doserror;
  653. end; { RunEXECOM }
  654.  
  655. function RunDOS(Msg:string):integer;
  656. {}
  657. var Comspec:string;
  658. begin
  659.    Comspec := GetEnv('COMSPEC');
  660.    swapvectors;
  661.    writeln;
  662.    writeln(Msg);
  663.    exec(comspec,'');
  664.    SwapVectors;
  665.    RunDOS := doserror;
  666. end; { RunDOS }
  667.  
  668. function ParamLine: String;
  669. {returns the command line as a space delimited string}
  670. var I: integer;
  671.     P: integer;
  672.     Line: string;
  673. begin
  674.    Line := '';
  675.    P := ParamCount;
  676.    if P > 0 then
  677.       for I := 1 to P do
  678.           Line := Line + ParamStr(I) + ' ';
  679.    ParamLine := Line;
  680. end; { ParamLine }
  681.  
  682. function ParamVal(Marker:string): string;
  683. {searches for Marker in string and returns the characters following}
  684. var ValStr,
  685.     Line: string;
  686.     Loc1, Loc2: integer;
  687. begin
  688.    Line := ParamLine;
  689.    ValStr := '';
  690.    if Line <> '' then
  691.    begin
  692.       Loc1 := pos(SetUpper(Marker),SetUpper(Line));
  693.       if Loc1 = 0 then {not found}
  694.          ValStr := ''
  695.       else
  696.       begin
  697.          Loc1 := Loc1 + length(Marker);
  698.          if (Loc1 > Length(Line))
  699.          or (Line[Loc1] = Marker[1]) then
  700.             ValStr := ''
  701.          else
  702.          begin
  703.             Loc2 := Loc1;
  704.             repeat
  705.                inc(Loc2)
  706.             until (Line[Loc2] = Marker[1])
  707.                or (Loc2 > length(Line));
  708.             ValStr := Copy(Line,Loc1,Loc2-Loc1);
  709.          end;
  710.       end;
  711.    end;
  712.    ParamVal := ValStr;
  713. end; { ParamVal }
  714.  
  715. function Frequency(Match:string;Source:string): byte;
  716. {returns the number of times that Match occurs in SOURCE}
  717. var Len,Loc, Counter: byte;
  718. begin
  719.    Counter := 0;
  720.    Len := Length(match);
  721.    if (Len <> 0) and (length(Source) > 0) then
  722.       repeat
  723.          Loc := pos(Match,Source);
  724.          if Loc <> 0 then
  725.          begin
  726.             inc(Counter);
  727.             delete(Source,Loc,length(Match));
  728.          end;
  729.       until Loc = 0;
  730.    Frequency := Counter;
  731. end; { Frequency }
  732.  
  733. function BadCharPos(Str:string): integer;
  734. {Pass either a path or file+ext}
  735. var I: integer;
  736. begin
  737.    with MiscVars do
  738.    begin
  739.       BadCharPos := 0;
  740.       for I := 1 to length(Str) do
  741.          if pos(Str[I],Illegal) <> 0 then
  742.          begin
  743.             BadCharPos := I;
  744.             exit;
  745.          end;
  746.    end;
  747. end; { BadCharPos }
  748.  
  749. function ValidFileName(FN:string): shortint;
  750. {Validates a file path and name and returns following
  751.  codes:
  752.           -2     Valid path, but no file specified
  753.           -1     Path and name OK but file does not exist
  754.            0     Path and name OK and file exists
  755.            1     Illegal drive specifier
  756.            2     Illegal characters in path
  757.            3     Invalid Path
  758.            4     No file specified
  759.            5     Illegal Characters in name
  760.            6     Name longer than eight characters
  761.            7     Extension longer than three characters
  762. }
  763. var ECode: shortint;
  764.     OldDIR,D,P,F,E: string;
  765.     Loc: byte;
  766.     Inf: SearchRec;                                {1.00b}
  767.  
  768.     function Legal(Str:string;AllowSlash:boolean): boolean;
  769.     {}
  770.     var I: integer;
  771.     begin
  772.        Legal := BadCharPos(Str) = 0;
  773.        if not AllowSlash then
  774.           if pos('\',Str) > 0 then
  775.              legal := false;
  776.     end;
  777.  
  778. begin
  779.    ECode := 0;
  780.    Loc := pos(':',FN);
  781.    if Loc = 0 then
  782.    begin
  783.       D := '';
  784.       P := FN;
  785.    end else
  786.    begin
  787.       D := SetUpper(copy(FN,1,Loc));
  788.       P := copy(FN,succ(Loc),255);
  789.       if (Loc <> 2) or ((D[1] in ['A'..'Z'])=false) then
  790.       begin
  791.          ValidFileName := 1;
  792.          exit;
  793.       end;
  794.    end;
  795.    Loc := LastPos('\',P);
  796.    if Loc = 0 then
  797.    begin
  798.       F := P;
  799.       P := '';
  800.    end else
  801.    begin
  802.       F := copy(P,succ(Loc),255);
  803.       P := copy(P,1,pred(Loc));
  804.    end;
  805.    Loc := pos('.',F);
  806.    if Loc = 0 then
  807.       E := ''
  808.    else
  809.    begin
  810.       E := copy(F,succ(Loc),255);
  811.       F := copy(F,1,pred(Loc));
  812.    end;
  813.    if not legal(P,true) then
  814.       Ecode := 2
  815.    else
  816.    begin
  817.       if D+P <> '' then
  818.       begin
  819.          GetDir(0,OldDir);
  820.          {$I-}
  821.          ChDir(D+P);
  822.          {$I+}
  823.          if IOResult <> 0 then
  824.          begin
  825.             ValidFileName := 3;
  826.             ChDir(OldDir);  {1.00d}
  827.             exit;
  828.          end else
  829.             ChDir(OldDir);
  830.       end;
  831.       if (F='') and (E='') then
  832.          Ecode := 4
  833.       else
  834.       begin
  835.          if not Legal(F+E,false) then
  836.             Ecode := 5
  837.          else
  838.          begin
  839.             if length(F) > 8 then
  840.                Ecode := 6
  841.             else if length(E) > 3 then
  842.                Ecode := 7;
  843.          end;
  844.       end;
  845.    end;
  846.    if Ecode = 0 then
  847.    begin
  848.       if not Exist(FN) then
  849.          ECode := -1
  850.       else
  851.       begin
  852.          findfirst(FN,Directory,Inf);
  853.          if (DOSError <> 0) or ((DOSError = 0) and (Inf.Attr = Directory)) then
  854.             ECode := -2;
  855.       end
  856.    end;
  857.    ValidFileName := Ecode;
  858. end; { ValidFileName }
  859.  
  860. function GetMin(Value1,Value2:longint): longint;
  861. {returns the smallest of two Values}
  862. begin
  863.    if Value1 > Value2 then
  864.       GetMin := Value2
  865.    else
  866.       GetMin := Value1;
  867. end; { GetMin }
  868.  
  869. function GetMax(Value1,Value2:longint): longint;
  870. {returns the larger of two Values}
  871. begin
  872.    if Value1 > Value2 then
  873.       GetMax := Value1
  874.    else
  875.       GetMax := Value2;
  876. end; { GetMax }
  877.  
  878. procedure HeapRecord;
  879. {}
  880. begin
  881.    with MiscVars do
  882.    begin
  883.       MiscVars.StartMem := MemAvail;
  884.       HeapIsRecorded := true;
  885.    end;
  886. end; { HeapRecord }
  887.  
  888. procedure HeapCheck;
  889. {}
  890. var Ch: char;
  891. begin
  892.    with MiscVars do
  893.    begin
  894.       if MemAvail <> MiscVars.StartMem then
  895.       begin
  896.          ClrScr;
  897.          if HeapIsRecorded then
  898.          begin
  899.             writeln('MEMORY ERROR - Starting Free memory: ',MiscVars.StartMem);
  900.             writeln('             - Current  Free memory: ',MemAvail);
  901.             writeln('                              delta: ',MiscVars.StartMem - MemAvail);
  902.             writeln('Press any key ...');
  903.             ch := Readkey;
  904.             halt(99);
  905.          end else
  906.          writeln(HeapCheckErrMsg);
  907.       end;
  908.       HeapIsRecorded := false;
  909.    end;
  910. end; { HeapCheck }
  911.  
  912.               {*********************************************}
  913.               {**  U N I T   I N I T I A L I Z A T I O N  **}
  914.               {*********************************************}
  915. procedure MiscDefaultSettings;
  916. {}
  917. begin
  918.    with MiscVars do
  919.    begin
  920.       GoldMemBuffer := 10000;
  921.       HeapIsRecorded := false;
  922.       BeepOn := true;
  923.       LPTport := 0;  {LPT1}
  924.       HeapCheckErrMsg := 'HeapCheck ERROR! - Heap was not previously recorded.';
  925.    end;
  926. end; { MiscDefaultSettings }
  927.  
  928.  
  929. procedure GoldMiscInit;
  930. {}
  931. begin
  932.    with MiscVars do
  933.    begin
  934.       ECode := 0;
  935.       EMsgFunc := MiscEMsg;
  936.    end;
  937.    MiscDefaultSettings;
  938. end; { GoldMiscInit }
  939.  
  940. {$IFDEF TTT5}
  941.  
  942. function  File_Size(Filename:string): longint;
  943. {included for TTT5 compatibility}
  944. begin
  945.    File_Size := FSize(Filename);
  946. end; { FIle_Size }
  947.  
  948. function  File_Drive(Full:string): string;
  949. {included for TTT5 compatibility}
  950. begin
  951.    File_Drive := FileDrive(Full);
  952. end; { File_Drive }
  953.  
  954. function  File_Directory(Full:string): string;
  955. {included for TTT5 compatibility}
  956. begin
  957.    File_Directory := FileDirectory(Full);
  958. end; { File_Directory }
  959.  
  960. function  File_Name(Full:string): string;
  961. {included for TTT5 compatibility}
  962. begin
  963.    File_Name := FileName(Full);
  964. end; { File_Name }
  965.  
  966. function  File_Ext(Full:string): String;
  967. {included for TTT5 compatibility}
  968. begin
  969.    File_Ext := FileExt(Full);
  970. end; { File_Ext }
  971.  
  972. function  Printer_Status:byte;
  973. {included for TTT5 compatibility}
  974. begin
  975.    Printer_Status := PrinterStatus;
  976. end; { Printer_Status }
  977.  
  978. function  Alternate_Printer_Status:byte;
  979. {included for TTT5 compatibility}
  980. begin
  981.    Alternate_Printer_Status := AlternatePrinterStatus;
  982. end; { Alternate_Printer_Status }
  983.  
  984. function  Printer_ready:boolean;
  985. {included for TTT5 compatibility}
  986. begin
  987.    Printer_Ready := PrinterReady;
  988. end; { Printer_ready }
  989.  
  990. procedure Reset_Printer;
  991. {included for TTT5 compatibility}
  992. begin
  993.    ResetPrinter;
  994. end; { Reset_Printer }
  995.  
  996. {$ENDIF}
  997.  
  998. begin
  999.    GoldMiscInit;
  1000. end.
  1001.